package com.android.xueshengzhouzhi;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.android.until.Until;
import com.umeng.analytics.MobclickAgent;

import android.text.Html.ImageGetter;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.text.Html;

public class NewsShow extends Activity {

	private TextView tv_content;
	private TextView tv_url;
	private Intent intent;
	private String content;
	private ProgressDialog dlog;

	ActionBar action;

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				// 关闭ProgressDialog
				dlog.dismiss();
				// 更新UI
				ImageGetter imageGetter = new ImageGetter() {
					@Override
					public Drawable getDrawable(final String source) {
						final Drawable[] drawable = new Drawable[2];
						new Thread() {
							public void run() {
								try {
									drawable[0] = Drawable.createFromStream(
											new URL(source).openStream(), "");
									drawable[0].setBounds(0, 0,
											drawable[0].getIntrinsicWidth(),
											drawable[0].getIntrinsicHeight());
								} catch (MalformedURLException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}.start();
						while (drawable[0] == null) {
						}
						return drawable[0];
					}
				};
				tv_content.setText(Html.fromHtml(content, imageGetter, null));
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_show);
		tv_content = (TextView) findViewById(R.id.tv_content);
		tv_url = (TextView) findViewById(R.id.tv_url);

		getList();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onResume(NewsShow.this);
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		getList();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onResume(NewsShow.this);
	}

	public void getList() {
		if (Until.isNetworkConnected(getApplicationContext()) != false) {
			dlog = ProgressDialog.show(NewsShow.this, "Loading.......",
					"please wait......", true, false);
			action = getActionBar();
			action.setTitle("周知详情");
			intent = getIntent();
			final String url = intent.getStringExtra("url");
			tv_url.setText(Html.fromHtml("<u>" + "查看原网页" + "</u>"));
			if (url.substring(0, 3).equals("http") != true) {
				new Thread() {
					public void run() {
						content = com.android.until.Until.DeleteHtml(url);
						handler.obtainMessage(1).sendToTarget();
					}
				}.start();
				tv_url.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent inteit = new Intent(Intent.ACTION_VIEW, Uri
								.parse("http://ssdut.dlut.edu.cn" + url));
						startActivity(inteit);
					}
				});
			} else {
				new Thread() {
					public void run() {
						content = com.android.until.Until.Get_(url);
						handler.obtainMessage(1).sendToTarget();
					}
				}.start();
				tv_url.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent inteit = new Intent(Intent.ACTION_VIEW, Uri
								.parse(url));
						startActivity(inteit);
					}
				});
			}
		} else {
			showDiolog();

		}
	}

	public void showDiolog() {
		AlertDialog dlog = new AlertDialog.Builder(NewsShow.this)
				.setTitle("提示")
				.setIconAttribute(android.R.attr.alertDialogIcon)
				.setMessage("您的网络连接尚未开启，要现在开启么？")
				.setPositiveButton("取消", null)
				.setNegativeButton("确认", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent(
								Settings.ACTION_DATA_ROAMING_SETTINGS);
						startActivityForResult(intent, 0);
					}
				})
				.setNeutralButton("退出", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						NewsShow.this.finish();
					}
				}).create();
		dlog.setCanceledOnTouchOutside(false);
		dlog.show();

	}

}
